package com.hongawen.practise;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.hongawen.pojo.SteadyQualifyDTO;
import com.hongawen.pojo.SteadyResult;
import com.hongawen.pojo.TestMax;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年04月06日 19:34
 */
public class TestMaxService {

    private static List<SteadyQualifyDTO> steadyQualifyDTOS = new ArrayList<>();

    static {
        SteadyQualifyDTO steadyQualifyDTO;
        for (int i = 0; i < 6; i++) {
            int[] ints = NumberUtil.generateRandomNumber(1, 1440, 13);
            steadyQualifyDTO = new SteadyQualifyDTO();
            steadyQualifyDTO.setMYINDEX("line" + i);
            steadyQualifyDTO.setTime("2022-03-01 00:00:00");
            steadyQualifyDTO.setFlicker_OverTime(ints[0]);
            steadyQualifyDTO.setFreq_Dev_OverTime(ints[1]);
            steadyQualifyDTO.setVoltage_Dev_OverTime(ints[2]);
            steadyQualifyDTO.setUAberrance_OverTime(ints[3]);
            steadyQualifyDTO.setUHarm_2_OverTime(ints[4]);
            steadyQualifyDTO.setUHarm_3_OverTime(ints[5]);
            steadyQualifyDTO.setUHarm_4_OverTime(ints[6]);
            steadyQualifyDTO.setIHarm_2_OverTime(ints[7]);
            steadyQualifyDTO.setIHarm_3_OverTime(ints[8]);
            steadyQualifyDTO.setIHarm_4_OverTime(ints[9]);
            steadyQualifyDTO.setInUHARM_1_OverTime(ints[10]);
            steadyQualifyDTO.setInUHARM_2_OverTime(ints[11]);
            steadyQualifyDTO.setInUHARM_3_OverTime(ints[12]);
            steadyQualifyDTOS.add(steadyQualifyDTO);
        }

        for (int i = 0; i < 6; i++) {
            int[] ints = NumberUtil.generateRandomNumber(1, 1440, 13);
            steadyQualifyDTO = new SteadyQualifyDTO();
            steadyQualifyDTO.setMYINDEX("line" + i);
            steadyQualifyDTO.setTime("2022-03-02 00:00:00");
            steadyQualifyDTO.setFlicker_OverTime(ints[0]);
            steadyQualifyDTO.setFreq_Dev_OverTime(ints[1]);
            steadyQualifyDTO.setVoltage_Dev_OverTime(ints[2]);
            steadyQualifyDTO.setUAberrance_OverTime(ints[3]);
            steadyQualifyDTO.setUHarm_2_OverTime(ints[4]);
            steadyQualifyDTO.setUHarm_3_OverTime(ints[5]);
            steadyQualifyDTO.setUHarm_4_OverTime(ints[6]);
            steadyQualifyDTO.setIHarm_2_OverTime(ints[7]);
            steadyQualifyDTO.setIHarm_3_OverTime(ints[8]);
            steadyQualifyDTO.setIHarm_4_OverTime(ints[9]);
            steadyQualifyDTO.setInUHARM_1_OverTime(ints[10]);
            steadyQualifyDTO.setInUHARM_2_OverTime(ints[11]);
            steadyQualifyDTO.setInUHARM_3_OverTime(ints[12]);
            steadyQualifyDTOS.add(steadyQualifyDTO);
        }


        for (int i = 0; i < 6; i++) {
            int[] ints = NumberUtil.generateRandomNumber(1, 1440, 13);
            steadyQualifyDTO = new SteadyQualifyDTO();
            steadyQualifyDTO.setMYINDEX("line" + i);
            steadyQualifyDTO.setTime("2022-03-03 00:00:00");
            steadyQualifyDTO.setFlicker_OverTime(ints[0]);
            steadyQualifyDTO.setFreq_Dev_OverTime(ints[1]);
            steadyQualifyDTO.setVoltage_Dev_OverTime(ints[2]);
            steadyQualifyDTO.setUAberrance_OverTime(ints[3]);
            steadyQualifyDTO.setUHarm_2_OverTime(ints[4]);
            steadyQualifyDTO.setUHarm_3_OverTime(ints[5]);
            steadyQualifyDTO.setUHarm_4_OverTime(ints[6]);
            steadyQualifyDTO.setIHarm_2_OverTime(ints[7]);
            steadyQualifyDTO.setIHarm_3_OverTime(ints[8]);
            steadyQualifyDTO.setIHarm_4_OverTime(ints[9]);
            steadyQualifyDTO.setInUHARM_1_OverTime(ints[10]);
            steadyQualifyDTO.setInUHARM_2_OverTime(ints[11]);
            steadyQualifyDTO.setInUHARM_3_OverTime(ints[12]);
            steadyQualifyDTOS.add(steadyQualifyDTO);
        }


    }


    /**
     * @param args
     */
    public static void main(String[] args) {
//        getDataSingleMonitorSingeDay("line1", "2022-03-01 00:00:00");
        getDataSingleMonitorMoreDay("line1");
//        getDataMoreMonitorMoreDay();
    }


    /**
     * 监测点日统计
     * 计算监测点line1在3月1日的各项合格率
     */
    public static SteadyResult getDataSingleMonitorSingeDay(String lineId, String time) {
        SteadyResult steadyResult = new SteadyResult();
        List<SteadyQualifyDTO> steadyQualifyDTOList = steadyQualifyDTOS.stream()
                .filter(temp -> temp.getMYINDEX().equalsIgnoreCase(lineId) && temp.getTime().equalsIgnoreCase(time))
                .collect(Collectors.toList());
        if (!CollectionUtil.isEmpty(steadyQualifyDTOList)) {
            SteadyQualifyDTO steadyQualifyDTO = steadyQualifyDTOList.get(0);
            //计算闪变合格率
            steadyResult.setFlick(calculate(steadyQualifyDTO.getFlicket_AllTime(), steadyQualifyDTO.getFlicker_OverTime()));
            //频率偏差合格率
            steadyResult.setFrequency(calculate(steadyQualifyDTO.getAllTime(), steadyQualifyDTO.getFreq_Dev_OverTime()));
            //计算电压偏差
            steadyResult.setVoltageDeviation(calculate(steadyQualifyDTO.getAllTime(), steadyQualifyDTO.getVoltage_Dev_OverTime()));
            //谐波电压
            steadyResult.setHarmVoltage(calculate(steadyQualifyDTO.getAllTime(), NumberUtil.max(steadyQualifyDTO.getUAberrance_OverTime(), steadyQualifyDTO.getUHarm_2_OverTime(), steadyQualifyDTO.getUHarm_3_OverTime(), steadyQualifyDTO.getUHarm_4_OverTime())));
            //谐波电流
            steadyResult.setHarmElec(calculate(steadyQualifyDTO.getAllTime(), NumberUtil.max(steadyQualifyDTO.getIHarm_2_OverTime(), steadyQualifyDTO.getIHarm_3_OverTime(), steadyQualifyDTO.getIHarm_4_OverTime())));
            //简谐波电压含有率
            steadyResult.setInUharm(calculate(steadyQualifyDTO.getAllTime(), NumberUtil.max(steadyQualifyDTO.getInUHARM_1_OverTime(), steadyQualifyDTO.getInUHARM_2_OverTime(), steadyQualifyDTO.getInUHARM_3_OverTime())));
        }
        System.out.println("计算监测点line1在" + time + "的各项合格率:" + steadyResult);
        return steadyResult;
    }


    /**
     * 监测点时间范围内统计
     * <p>
     * 计算监测点line1在3月1日到3月3日期间的合格率
     */
    public static SteadyResult getDataSingleMonitorMoreDay(String lineId) {
        SteadyResult steadyResult = new SteadyResult();

        //从假数据里筛选出指定监测点的数据
        List<SteadyQualifyDTO> steadyQualifyDTOList = steadyQualifyDTOS.stream()
                .filter(temp -> temp.getMYINDEX().equalsIgnoreCase(lineId))
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(steadyQualifyDTOList)) {
            List<SteadyResult> steadyResults = new ArrayList<>();
            //获取出时间和id，调用第一个方法
            for (SteadyQualifyDTO steadyQualifyDTO : steadyQualifyDTOList) {
                steadyResults.add(getDataSingleMonitorSingeDay(lineId, steadyQualifyDTO.getTime()));
            }
            //求各项平均值
            steadyResult.setFlick(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getFlick).average().orElse(100), 2).doubleValue());
            steadyResult.setFrequency(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getFrequency).average().orElse(100), 2).doubleValue());
            steadyResult.setVoltageDeviation(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getVoltageDeviation).average().orElse(100), 2).doubleValue());
            steadyResult.setHarmVoltage(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getHarmVoltage).average().orElse(100), 2).doubleValue());
            steadyResult.setHarmElec(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getHarmElec).average().orElse(100), 2).doubleValue());
            steadyResult.setInUharm(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getInUharm).average().orElse(100), 2).doubleValue());
        }
        System.out.println("计算监测点line1在3月1日到3月3日期间的合格率:" + steadyResult);
        return steadyResult;
    }


    /**
     * 区域监测点日统计
     * <p>
     * 计算监测点line1 ~ line5在3月1日的各项合格率
     */
    public static SteadyResult getDataMoreMonitorSingleDay(List<String> lineIds, String time) {
        SteadyResult steadyResult = new SteadyResult();
        List<SteadyResult> steadyResults = new ArrayList<>();
        for (String lineId : lineIds) {
            steadyResults.add(getDataSingleMonitorSingeDay(lineId, time));
        }
        if (CollectionUtil.isNotEmpty(steadyResults)) {
            //求各项平均值
            steadyResult.setFlick(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getFlick).average().orElse(100), 2).doubleValue());
            steadyResult.setFrequency(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getFrequency).average().orElse(100), 2).doubleValue());
            steadyResult.setVoltageDeviation(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getVoltageDeviation).average().orElse(100), 2).doubleValue());
            steadyResult.setHarmVoltage(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getHarmVoltage).average().orElse(100), 2).doubleValue());
            steadyResult.setHarmElec(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getHarmElec).average().orElse(100), 2).doubleValue());
            steadyResult.setInUharm(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getInUharm).average().orElse(100), 2).doubleValue());
        }
        System.out.println("计算监测点line1 ~ line5在" + time + "的各项合格率:" + steadyResult);
        return steadyResult;
    }


    /**
     * 区域监测点时间范围内统计
     * <p>
     * 计算监测点line1 ~ line5在3月1日到3月3日期间的合格率
     */
    public static SteadyResult getDataMoreMonitorMoreDay() {
        SteadyResult steadyResult = new SteadyResult();
        List<SteadyResult> steadyResults = new ArrayList<>();
        //以时间分组的各项数据
        Map<String, List<SteadyQualifyDTO>> groupByTimeData = steadyQualifyDTOS.stream().collect(Collectors.groupingBy(SteadyQualifyDTO::getTime));
        if (CollectionUtil.isNotEmpty(groupByTimeData)) {
            Set<String> times = groupByTimeData.keySet();
            for (String time : times) {
                List<String> ids = groupByTimeData.get(time).stream().map(SteadyQualifyDTO::getMYINDEX).collect(Collectors.toList());
                steadyResults.add(getDataMoreMonitorSingleDay(ids, time));
            }
        }
        if (CollectionUtil.isNotEmpty(steadyResults)) {
            //求各项平均值
            steadyResult.setFlick(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getFlick).average().orElse(100), 2).doubleValue());
            steadyResult.setFrequency(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getFrequency).average().orElse(100), 2).doubleValue());
            steadyResult.setVoltageDeviation(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getVoltageDeviation).average().orElse(100), 2).doubleValue());
            steadyResult.setHarmVoltage(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getHarmVoltage).average().orElse(100), 2).doubleValue());
            steadyResult.setHarmElec(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getHarmElec).average().orElse(100), 2).doubleValue());
            steadyResult.setInUharm(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyResult::getInUharm).average().orElse(100), 2).doubleValue());
        }

        System.out.println("计算监测点line1 ~ line5在3月1日到3月3日期间的合格率:" + steadyResult);
        return steadyResult;
    }


    private static double calculate(Integer allTime, Integer overTime) {
        double standard = allTime - overTime;
        return NumberUtil.round((standard * 100) / allTime, 2).doubleValue();
    }


}
